"""
 -*- coding: utf-8-*-
 @File: mgt_logger.py
 @Author: chen
 @time: 2023/12/01/ 19:17
"""
import datetime
import logging
import os.path

cur_path = os.path.dirname(os.path.abspath(__file__))
now = datetime.datetime.now()
cur_date = now.strftime("%Y-%m-%d")
# 定义日志记录器
logger = logging.getLogger(os.path.basename(__file__))
# 设置日志输出级别
logger.setLevel(logging.DEBUG)
# 设置日志输出格式
fmt = logging.Formatter("%(asctime)s|%(filename)s-l%(lineno)s|%(levelname)s|msg:%(msg)s")
# 定义控制台日志处理器
handler_console = logging.StreamHandler()
handler_console.setLevel(logging.DEBUG)
handler_console.setFormatter(fmt)
logger.addHandler(handler_console)

# 定义文件日志处理器
log_dir = os.path.join(cur_path, "../logs")
if not os.path.exists(log_dir):
    os.makedirs(log_dir)
log_name = f"{cur_date}.log"
log_file = os.path.join(log_dir, log_name)
handler_file = logging.FileHandler(filename=log_file, encoding="utf8")
handler_file.setLevel(logging.DEBUG)
handler_file.setFormatter(fmt)
logger.addHandler(handler_file)
